package com.shs.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shs.bus.entity.Goods;
import com.shs.bus.entity.Provider;
import com.shs.bus.service.GoodsService;
import com.shs.bus.service.ProviderService;
import com.shs.bus.vo.GoodsVo;
import com.shs.bus.vo.ProviderVo;
import com.shs.sys.common.Constants;
import com.shs.sys.common.DataGridView;
import com.shs.sys.common.ResultObj;
import com.shs.sys.common.SHFileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jxm
 * @since 2020-04-16
 */
@RestController
@RequestMapping("goods")
public class GoodsController{

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ProviderService providerService;

    /**
     * 查询商品
     * @return
     */
    @RequestMapping("loadAllGoods")
    public DataGridView loadAllGoods(GoodsVo vo){
        Page<Goods> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Goods> wrapper = new QueryWrapper();
        wrapper.eq(vo.getProviderid()!=null && vo.getProviderid()!=0,"providerid",vo.getProviderid());
        wrapper.like(StringUtils.isNotBlank(vo.getGoodsname()),"goodsname",vo.getGoodsname());
        wrapper.like(StringUtils.isNotBlank(vo.getProductcode()),"productcode",vo.getProductcode());
        wrapper.like(StringUtils.isNotBlank(vo.getPromitcode()),"promitcode",vo.getPromitcode());
        wrapper.like(StringUtils.isNotBlank(vo.getDescription()),"description",vo.getDescription());
        wrapper.like(StringUtils.isNotBlank(vo.getSize()),"size",vo.getSize());
        wrapper.orderByDesc("id");
        goodsService.page(page,wrapper);
        List<Goods> records = page.getRecords();
        for (Goods record : records) {
            Provider provider = providerService.getById(record.getProviderid());
            if(null!=provider){
                record.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(page.getTotal(),records);
    }

    /**
     * 添加商品
     * @param vo
     * @return
     */
    @RequestMapping("addGoods")
    public ResultObj addGoods(GoodsVo vo){
        try {
            if(vo.getGoodsimg()!=null && vo.getGoodsimg().endsWith("_temp")){
                //去掉_temp后缀
                String nameFile = SHFileUtils.reNameFile(vo.getGoodsimg());
                vo.setGoodsimg(nameFile);
            }
            goodsService.save(vo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改商品
     * @param vo
     * @return
     */
    @RequestMapping("updateGoods")
    public ResultObj updateGoods(GoodsVo vo){
        try {
            //说明不是默认图片
            if(!(vo.getGoodsimg()!=null && vo.getGoodsimg().equals(Constants.IMAGES_DEFAULTGOODSIMG_PNG))){
                if(vo.getGoodsimg().endsWith("_temp")){
                    //去掉_temp后缀
                    String nameFile = SHFileUtils.reNameFile(vo.getGoodsimg());
                    vo.setGoodsimg(nameFile);
                    //删除原先的图片
                    Goods goods = goodsService.getById(vo.getId());
                    String oldPath = goods.getGoodsimg();
                    SHFileUtils.removeFileByPath(oldPath);
                }
            }
            goodsService.updateById(vo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("deleteGoods")
    public ResultObj deleteGoods(Integer id,String goodsimg){
        try {
            //删除原先文件
            SHFileUtils.removeFileByPath(goodsimg);
            goodsService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 查询所有商品
     * @return
     */
    @RequestMapping("loadAllGoodsForSelect")
    public DataGridView loadAllGoodsForSelect(){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("available",Constants.AVAILABLE_TRUE);
        List<Goods> list = goodsService.list(queryWrapper);
        for (Goods goods : list) {
            Provider provider = providerService.getById(goods.getProviderid());
            if(null!=provider){
                goods.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(list);
    }
    /**
     * 根据供应商查找商品
     * @return
     */
    @RequestMapping("loadGoodsByProviderId")
    public DataGridView loadGoodsByProviderId(Integer providerid){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("available",Constants.AVAILABLE_TRUE);
        queryWrapper.eq("providerid",providerid);
        List<Goods> list = goodsService.list(queryWrapper);
        for (Goods goods : list) {
            Provider provider = providerService.getById(goods.getProviderid());
            if(null!=provider){
                goods.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(list);
    }


}
